package javaLearning.数据库连接池.ConnectionPool.run;

import javaLearning.数据库连接池.ConnectionPool.Pool.ConnectionManager;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author : K k
 * @date : 23:05 2020/10/11
 */
public class testpool {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {

        List<Thread> threadlist = new ArrayList<Thread>();
        int time=0;
        String sql1 = "select * from item where id=6";
        String sql2 = "select * from item where id=7";
        for (int i = 0; i <= 10; i++) {
            Thread subThread = new Thread(new workrun(sql1));
            Thread subThread2 = new Thread(new workrun(sql2));
            subThread.start();
            subThread2.start();
            threadlist.add(subThread);
            threadlist.add(subThread2);
        }
        time++;
        for (Iterator<Thread> iterator = threadlist.iterator(); iterator.hasNext(); ) {
            Thread thread = iterator.next();
            thread.join();
        }
        //ConnectionManager.getInstance().destroy();

    }

}

class workrun implements Runnable {
    int i;
    String sql;
    public workrun(String sql) {
        this.sql=sql;
    }

    @Override
    public void run() {
        ConnectionManager cm = ConnectionManager.getInstance();

        //1.从数据池中获取数据库连接, 此时的connection是通过动态代理方式增强的
        Connection conn = cm.getConnection("default");
        if (conn==null){
            //由于获取时间超时而作废
            throw new RuntimeException("超时，连接作废");
        }
        System.out.println("线程 " + Thread.currentThread().getName() + "获得连接：" + conn);
        //模拟查询耗时操作
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //2.获取用于向数据库发送sql语句的statement
        Statement st = null;
        try {
            st = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //3.向数据库发sql,并获取代表结果集的resultset
        //4.取出结果集的数据
        ResultSet rs = null;
        try {
            rs = st.executeQuery(sql);
            while (rs.next()) {
                System.out.println("itemName=" + rs.getObject("name"));
                //System.out.println("price=" + rs.getObject("price"));
                //System.out.println("cateid=" + rs.getObject("cateid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //模拟查询耗时操作
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //5.关闭链接，释放资源
        try {
            rs.close();
            st.close();
            //conn.close();
            //动态代理来关闭
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


}
